/*
 *  Copyright (C) 1993  Robert Davis
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of Version 2, or any later version, of 
 *  the GNU General Public License as published by the Free Software 
 *  Foundation.
 */


static char RCSId[]="$Id: TicOptionsPanel.m,v 1.13 1993/05/29 04:32:56 davis Exp $";


#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/MenuCell.h>
#import <appkit/PopUpList.h>
#import <appkit/Matrix.h>

#import "AutoTicsPane.h"
#import "Gnuplot.h"
#import "GnuplotPlot.h"
#import "SeriesTicsPane.h"
#import "Status.h"
#import "StatusTics.h"
#import "TicOptionsPanel.h"
#import	"UserTicsPane.h"

@interface TicOptionsPanel (Private)
- _setupPane:(Pane *)aPane;
- _selectPane:(int)aPane;
- _swapPane:(Pane *)new;
- _updatePanel;			/** Overridden from OptionsPanel (Private) **/
@end


@implementation TicOptionsPanel

- init
{
    [super init];

    [NXApp loadNibSection: "TicOptionsPanel.nib"
		    owner: self
		withNames: NO
		 fromZone: [self zone]];

    [panel setFrameUsingName:"TicOptionsPanel"];
    [panel setFrameAutosaveName:"TicOptionsPanel"];

    return self;
}


- free
{
    [seriesPane free];
    [userPane free];
    [autoPane free];
    [[coordButton target] free];

    return [super free];
}


- changeCoord:sender
{
    [coordButton setTag:[sender selectedTag]];
    [self _updatePanel];
    return self;
}


- (int)coord
{
    PopUpList	*popUp = [coordButton target];
    Matrix	*matrix = [popUp itemList];
    int		tag = [[matrix selectedCell] tag];

    return tag;
}


- doSetTicsType:sender
{
    int		tag = [sender selectedTag];

    [status setTicTypeCoord:[[coordButton target] indexOfItem:
			     [coordButton title]] to:tag];
    [typeButton setTag:tag];
    [self _updatePanel];
    return self;
}



/*  
 *  Overridden because the superclass (OptionsPanel) only checks to 
 *  see if the current status has changed -- we need to know if the 3D 
 *  state of the doc has changed, too.
 */
- windowDidUpdate:sender
{
    static BOOL	wasThreeD = NO;
    id		mainWindow = [NXApp mainWindow];
    Status	*newStatus;
    BOOL	newThreeD = wasThreeD;

    if (mainWindow)
	newStatus = [[mainWindow delegate] status];
    else {
	id	doc;
	if (doc = [[NXApp delegate] currentDoc])
	    newStatus = [doc status];
	else
	    newStatus = nil;
    }

    /*  
     *  Update if the current status object is different or if the 3D 
     *  status has changed (so we can enable/disable the third choice 
     *  in the pop up list).
     */

    newThreeD = newStatus? [newStatus isThreeD] : wasThreeD;
    if ((newStatus != status) || (newThreeD != wasThreeD)) {
	status = newStatus;
	[self _updatePanel];
    }

    wasThreeD = newThreeD;
    return self;
}


// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}


@end





@implementation TicOptionsPanel (Private)

- _setupPane:(Pane *)aPane
{
    if (aPane) {
	View	*panesView = [aPane view];

	[[panel contentView] addSubview: panesView];
	[[panesView allocateGState] lockFocus];
	[panesView unlockFocus];
	[panesView moveTo:0:0];
    }

    return self;
}


- _selectPane:(int)aPane
{
    switch (aPane) {

    case TIC_SERIES:
	if (!seriesPane) {
	    seriesPane = [[SeriesTicsPane allocFromZone:[self zone]] init];
	    [self _setupPane:seriesPane];
	}
	[self _swapPane:seriesPane];
	break;

    case TIC_USER:
	if (!userPane) {
	    userPane = [[UserTicsPane allocFromZone:[self zone]] init];
	    [self _setupPane:userPane];
	}
	[self _swapPane:userPane];
	break;

    default:
	if (!autoPane) {
	    autoPane = [[AutoTicsPane allocFromZone:[self zone]] init];
	    [self _setupPane:autoPane];
	}
	[self _swapPane:autoPane];
	break;

    }

    [currentPane perform:@selector(selectControl:)
		    with:self
	      afterDelay:1
	  cancelPrevious:YES];

    return self;
}


- _swapPane:(Pane *)new
{
    /*
     *  If the new pane is not already visible, move it into the panel.
     */

    if (new != currentPane) {

	[[panel contentView] replaceSubview:[currentPane view]
				       with:[new view]];
	[currentPane didSwapOut:self];

	/*
	 *  Notice we send ourself as the doc so that the pane can
	 *  query us for such things as the current coordinate.
	 */
	[[new didSwapIn:self] updateStatus:status doc:self];

	[panel setTitle:[new title]];
	[panel setMiniwindowIcon:[new icon]];

	currentPane = new;

    }

    return self;
}




- _updatePanel
{
    int		coord = [coordButton tag];
    int		type = [typeButton tag];
    BOOL	isEnabled;

    [panel disableDisplay];

    /*
     *  If the current status is not nil, update the values of all the
     *  controls.
     */
    if (status) {

	MenuCell	*cell;
	BOOL		isThreeD = [status isThreeD];

	if (!isThreeD && (coord == Z_TAG))  {
	    cell = [[coordButton target] findCellWithTag:X_TAG];
	    [coordButton setTitle:[cell title]];
	    [coordButton setTag:coord = [cell tag]];
	}

	type = [status ticTypeCoord:coord];
	cell = [[typeButton target] findCellWithTag:type];
	[typeButton setTitle:[cell title]];
	[typeButton setTag:type];

	/*
	 *  Enabling/Disabling of the Z coordinate button in the pop
	 *  up list.
	 */
	
	[[[coordButton target] findCellWithTag:Z_TAG] setEnabled:isThreeD];

    }

    /* Enable/disable our controls */

    [coordButton setEnabled: isEnabled = (status? YES : NO)];
    [coordLabelField setEnabled: isEnabled];
    [typeButton setEnabled: isEnabled];
    [typeLabelField setEnabled: isEnabled];

    [self _selectPane:type];
    [currentPane updateStatus:status doc:self];

    [panel reenableDisplay];
    [panel display];

    return self;
}


@end
